package com.zhang.table;

import com.zhang.second.utils.SensorReading;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.DataTypes;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.descriptors.Json;
import org.apache.flink.table.descriptors.Kafka;
import org.apache.flink.table.descriptors.Schema;
import org.apache.kafka.clients.producer.ProducerConfig;

import static org.apache.flink.table.api.Expressions.$;

/**
 * @title:
 * @author: zhang
 * @date: 2022/2/10 21:21
 */
public class SinkKafka {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        SingleOutputStreamOperator<SensorReading> sensorReadingDS = env
                .socketTextStream("localhost", 9999)
                .map(new MapFunction<String, SensorReading>() {
                    @Override
                    public SensorReading map(String value) throws Exception {
                        String[] fields = value.split(",");
                        return new SensorReading(
                                fields[0], Integer.parseInt(fields[1])
                        );
                    }
                });

        //获取流表环境
        StreamTableEnvironment tableEnvironment = StreamTableEnvironment.create(env);

        //将流转换为动态表
        Table table = tableEnvironment.fromDataStream(sensorReadingDS);

        //使用table api
        Table select = table
                .select($("sensorId"), $("temperature"));

       // tableEnvironment.toChangelogStream(select).print();

        tableEnvironment
                .connect(new Kafka()
                        .version("universal")
                        .topic("test_flink")
                        .property(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "hadoop302:9092"))
                .withSchema(new Schema()
                        .field("sensorId", DataTypes.STRING())
                        .field("temperature", DataTypes.INT()))
                //.withFormat(new Csv())
                .withFormat(new Json())
                .createTemporaryTable("wordCount");
        //tableEnvironment.toRetractStream(select, Row.class).print();
        select.executeInsert("wordCount");


        env.execute();
    }
}
